﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KanbanizeAPI;

namespace KanbanizeAPIHarness
{
    public partial class Form1 : Form
    {
        KanbanizeConnector Connector;
        KanbanizeResultParser Parser;

        public Form1()
        {
            InitializeComponent();

            FormBorderStyle = FormBorderStyle.FixedSingle;

            Parser = new KanbanizeXmlResultParser();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Connector = new KanbanizeConnector(emailTextBox.Text, passwordTextBox.Text, apiKeyTextBox.Text);

            string result = Connector.Login();

            Append(result);

            KanbanizeLogin login = Parser.KanbanizeLoginFromLoginRequest(result);

            Append(login.EmailAddress);
            Append(login.Username);
            Append(login.RealName);
            Append(login.CompanyName);
            Append(login.TimeZone.ToString());
            Append(login.ApiKey);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string result = Connector.GetProjectsAndBoards();
            Append(result);

            List<KanbanizeProject> projects = Parser.ProjectsFromProjectsAndBoardsResult(result);

            foreach (KanbanizeProject project in projects)
            {
                Append(string.Format("{0} - Id: {1} Boards: {2}", project.Name, project.Id, project.Boards.Count));

                foreach (KanbanizeBoard board in project.Boards)
                    Append(string.Format("   Board: {0} Id: {1}", board.Name, board.Id));
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string result = Connector.GetBoardStructure(int.Parse(boardTextBox.Text));
            Append(result);

            KanbanizeBoard board = Parser.BoardStructureFromStructureResult(result);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string result = Connector.GetBoardSettings(int.Parse(boardTextBox.Text));
            Append(result);

            List<KanbanizeUser> userList = Parser.UsersFromSettingsResult(result);

            foreach (KanbanizeUser user in userList)
            {
                Append(user.Username);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            KanbanizeUser user = new KanbanizeUser();
            user.Username = usernameTextBox.Text;

            Append(Connector.CreateNewTask(int.Parse(boardTextBox.Text), "Test Task from .NET", "This is a test task description",
                TaskPriority.High, user, "99b399", 1, "Windows issue", 
                DateTime.Now.AddDays(7)));
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Append(Connector.DeleteTask(2, 171));
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Append(Connector.GetTaskDetails(2, 163));
        }

        private void button8_Click(object sender, EventArgs e)
        {
            Append(Connector.GetAllTasks(2));
        }

        private void button9_Click(object sender, EventArgs e)
        {
            Append(Connector.AddComment(171, string.Format("Test comment added from library: " + DateTime.Now.ToString("yyyy-MM-dd hh:MM:ss"))));
        }

        private void button10_Click(object sender, EventArgs e)
        {
            Append(Connector.MoveTask(2, 172, "Backlog"));
        }

        private void button11_Click(object sender, EventArgs e)
        {
            KanbanizeUser user = new KanbanizeUser();
            user.Username = usernameTextBox.Text;

            Append(Connector.EditTask(int.Parse(boardTextBox.Text), int.Parse(cardTextBox.Text), "Test Task from .NET", "This is a test task description",
                TaskPriority.Low, user, "990000", 1, "windows networking email",
                DateTime.Now.AddDays(5)));
        }

        private void button12_Click(object sender, EventArgs e)
        {
            Append(Connector.BlockTask(int.Parse(boardTextBox.Text), int.Parse(cardTextBox.Text), BlockEvent.Unblock, "Testing card block from .NET via API!"));
        }

        private void Append(string s)
        {
            richTextBox1.AppendText(s + "\r\n");
            richTextBox1.Select(richTextBox1.TextLength, 0);
            richTextBox1.ScrollToCaret();
        }
    }
}
